﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using Caliburn.Micro;
using RatingAuthor;
using SamizdatSearch.Properties;

namespace SamizdatSearch
{
  [Export(typeof(IShell))]
  public class ShellViewModel : Screen, IShell 
  {
    static ShellViewModel()
    {
      Application.Current.Exit += (s, e) => Settings.Default.Save();
    }

    public PropertyChangedBase ActiveView
    {
      get { return _activeView; }
      set
      {
        if (_activeView == value) return;
        _activeView = value;
        NotifyOfPropertyChange(() => ActiveView);
      }
    }

    private PropertyChangedBase _activeView;


    private readonly IWindowManager _windowManager;

    [ImportingConstructor]
    public ShellViewModel(IWindowManager windowManager)
    {
      base.DisplayName = "Поиск на самиздате";
      _windowManager = windowManager;
      ActiveView = new LoadingDbViewModel(db => ActiveView = 
        new SearchResultsViewModel(this, db, _windowManager));
    }

    public void UpdateDb(SiDatabase db)
    {
      ActiveView = new UpdateDbViewModel(this, db);
    }

    public void DbUpdated(SiDatabase db)
    {
      ActiveView = new SearchResultsViewModel(this, db, _windowManager);
    }
  }
}